<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :node-data="{name:'江苏传智播客教育科技股份有限公司', manager: '负责人',id:''}" :is-root="true" @add="addDepartments" />
        <hr>
        <el-tree :data="treedata" default-expand-all :expand-on-click-node="false">
          <template #default="obj">
            <treeTools :node-data="obj.data" :is-root="false" @del="getDepartments" @add="addDepartments" @update="updateDepartments" />
          </template>
        </el-tree>
      </el-card>
      <addDepartments ref="addDept" :is-show-dialog="isShowDialog" :node-data="nodeData" @close="onClose" @add="getDepartments" @update="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/treeTools.vue'
import addDepartments from './components/addDepartments.vue'
import { getDepartments } from '@/api/departments'
import { ListToTreeData } from '@/utils/index'
export default {
  components: { treeTools, addDepartments },
  data() {
    return {
      treedata: [],
      isShowDialog: false,
      nodeData: {}
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      this.treedata = ListToTreeData(depts, '')
      console.log(depts)
    },
    // 显示添加部门的弹窗
    addDepartments(nodeData) {
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    // 关闭添加部门的弹窗
    onClose() {
      this.isShowDialog = false
    },
    // 编辑
    async updateDepartments(nodeData) {
      this.nodeData = nodeData
      await this.$refs.addDept.getDepartmentsDetail(nodeData)
      this.isShowDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
